load("@prelude//toolchains/android/tools:build_rules.bzl", "buck_java_binary", "buck_kotlin_library", "buck_prebuilt_artifact", "buck_prebuilt_jar")
load("@prelude//toolchains/android/tools/build_rules:fb_native.bzl", "fb_native")
load("@prelude//utils:source_listing.bzl", "source_listing")

oncall("android_devxx")

source_listing()

buck_kotlin_library(
    name = "cp_snapshot_generator_lib",
    srcs = glob(
        ["**/*.kt"],
    ),
    extra_kotlinc_arguments = ["-Xopt-in=kotlin.ExperimentalStdlibApi"],
    labels = ["codegen_rule"],  # see: https://fburl.com/workplace/fhud6fe6
    visibility = ["PUBLIC"],
    runtime_deps = [
        "prelude//toolchains/android/third-party:kotlin-build-tools-impl",
        "prelude//toolchains/android/third-party:kotlin-compiler-embeddable",
    ],
    deps = [
        "prelude//toolchains/android/src/com/facebook/buck/core/util/log:log",
        "prelude//toolchains/android/third-party:args4j",
        "prelude//toolchains/android/third-party:kotlin-build-tools-api",
    ],
)

buck_java_binary(
    name = "cp_snapshot_generator_binary",
    main_class = "com.facebook.buck.jvm.kotlin.buildtools.snapshot.Main",
    visibility = ["PUBLIC"],
    deps = [
        ":cp_snapshot_generator_lib",
    ],
)

fb_native.alias(
    name = "cp_snapshot_generator",
    actual = ":cp_snapshot_generator_prebuilt",
    visibility = ["PUBLIC"],
)

# This is a prebuilt jar so that it can be part of the buck2 bootstrap toolchain.
buck_prebuilt_jar(
    name = "cp_snapshot_generator_prebuilt",
    binary_jar = ":cp_snapshot_generator.jar",
    is_executable = True,
    should_generate_snapshot = False,
    visibility = ["PUBLIC"],
)

# To rebuild the jar and upload it to CAS, do:
# buck build prelude//toolchains/android/src/com/facebook/buck/jvm/kotlin/buildtools/snapshot:cp_snapshot_generator_binary --show-simple-output | xargs frecli --use-case android_build_infra_tools cas upload-blob
buck_prebuilt_artifact(
    name = "cp_snapshot_generator.jar",
    # @oss-disable[end= ]: cas_digest = "0febd203f9e94fa652a9e26c18d50b17ffc6d3ba23ccde84b2f785a25b4adacf:60666465",
    oss_sha1 = "1bbf0a79f5c88a4bb316ebd98dfca7b615aae52a",
    oss_url = "https://github.com/facebook/buck2/releases/download/androidToolchain%2F2025-04-03/cp_snapshot_generator.jar",
)
